import requests
import re

class Booktxt:

    headers = {
        'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36'
    }

    def __init__(self,
                 charset = 'gbk',
                 keyword = None,
                 search_url = 'http://zhannei.baidu.com/cse/search',
                 page_num = 1,
                 bookhome = None,
                 book_url = None,
                 chapter_url = None,
                 ):
        self.charset = charset
        self.home_url = 'https://www.booktxt.net/'

        if keyword != None:
            self.search_url = search_url
            self.keyword = keyword
            self.page_num = page_num
            self.s_html = self.search_html(self.keyword,self.page_num)

        if bookhome != None:
            self.html = self.html(self.home_url)
            self.home_book = self.home_book()

        if book_url != None:
            self.book_url = book_url
            self.html = self.html(self.book_url)
            self.book_info = self.book_info()

        if chapter_url != None:
            self.chapter_url = chapter_url
            self.html = self.html(self.chapter_url)
            self.chapter_info = self.chapter_info()


    def search_html(self,keyword,page_num):
        keyword = keyword.encode(self.charset)
        page_num -= 1
        params = {
            'q':keyword,
            'p':page_num,
            's':'5334330359795686106'
        }
        response = requests.get(self.search_url,headers = self.headers,params = params)
        response.encoding = 'utf-8'
        return response.text

    def novel_info(self):
        search_dict = {}
        name_re = '<a cpos="title" href="(.*?)" title="(.*?)" class="result-game-item-title-link" target="_blank">'
        author_re = '<span>([\s\S]*?)</span>'
        latest_re = '<a cpos="newchapter" href="(.*?)" class="result-game-item-info-tag-item" target="_blank">(.*?)</a>'
        type_re = '<span class="result-game-item-info-tag-title">(\w*?)</span>'
        time_re = '<span class="result-game-item-info-tag-title">([0-9].*?)</span>'
        img_re = '<img src="(.*?)" alt=".*?" class="result-game-item-pic-link-img"'
        name = re.findall(name_re,self.s_html)
        author = re.findall(author_re,self.s_html)
        latest = re.findall(latest_re,self.s_html)
        type = re.findall(type_re,self.s_html)
        time = re.findall(time_re,self.s_html)
        img = re.findall(img_re,self.s_html)
        for i in author:
            author[author.index(i)] = i.replace('\r\n','').replace(' ','')
        for i in name:
            num = name.index(i)
            search_dict[i[1]] = {'novel_author':author[num],
                                 'novel_author_url':None,
                                 'novel_url':i[0],
                                 'latest_chapter':latest[num][1],
                                 'latest_chapter_url':latest[num][0],
                                 'novel_type':type[num],
                                 'refresh_time':time[num],
                                 'novel_status':None,
                                 'novel_words':None,
                                 'novel_img':img[num]}
        return search_dict

    def html(self,xurl):
        response = requests.get(xurl,headers = self.headers)
        response.encoding = self.charset
        return response.text

    def home_book(self):
        home_dict = {}
        img_re = '<img src="(.*?)" alt=".*?"  width="120" height="150" />'
        au_url_name_re = '<dt><span>(.*?)</span><a href="(.*?)">(.*?)</a></dt>'
        bookintro_re = '\s<dd>(.*?)</dd>'
        type_re = '<meta property="og:novel:category" content="(.*?)"/>'
        time_re = '<meta property="og:novel:update_time" content="(.*?) "/>'
        leatest_re = '<dd><a href="(.*?)">(.*?)</a></dd>'
        status_re = '<meta property="og:novel:status" content="(.*?)"/>'
        img = re.findall(img_re,self.html)
        au_url_name = re.findall(au_url_name_re, self.html)
        bookintro = re.findall(bookintro_re,self.html)
        for i in au_url_name:
            num = au_url_name.index(i)
            response = requests.get(i[1],headers = self.headers)
            response.encoding = self.charset
            novel_html = response.text
            type = re.findall(type_re,novel_html)
            time = re.findall(time_re,novel_html)
            leatest = re.findall(leatest_re,novel_html)[0]
            status = re.findall(status_re,novel_html)
            home_dict[i[2]] = {'novel_author':i[0],
                               'novel_author_url':None,
                               'novel_url':i[1],
                               'novel_intro':bookintro[num],
                               'novel_type':type[0],
                               'refresh_time':time[0],
                               'novel_status':status[0],
                               'novel_words':None,
                               'novel_img':img[num],
                               'latest_chapter':leatest[1],
                               'latest_chapter_url':i[1] + leatest[0]
                               }

        return home_dict

    def book_info(self):
        name_re = '<h1>(.*?)</h1>'
        author_re = '<meta property="og:novel:author" content="(.*?)"/>'
        intro_re = '<meta property="og:description" content="(.*?)"/>'
        type_re = '<meta property="og:novel:category" content="(.*?)"/>'
        time_re = '<meta property="og:novel:update_time" content="(.*?) "/>'
        img_re = '<img src="(.*?)" width="152" height="195" />'
        leatest_re = '<dd><a href="(.*?)">(.*?)</a></dd>'
        status_re = '<meta property="og:novel:status" content="(.*?)"/>'
        name = re.findall(name_re,self.html)[0]
        author = re.findall(author_re,self.html)[0]
        intro = re.findall(intro_re,self.html)[0]
        type = re.findall(type_re,self.html)[0]
        time = re.findall(time_re,self.html)[0]
        status = re.findall(status_re,self.html)[0]
        img = re.findall(img_re,self.html)[0]
        leatest = re.findall(leatest_re,self.html)
        novel_dict = {'novel_name':name,
                      'novel_author':author,
                      'novel_author_url':None,
                      'novel_intro':intro,
                      'novel_type':type,
                      'refresh_time':time,
                      'novel_status':status,
                      'novel_words': None,
                      'novel_img': self.home_url + img,
                      'latest_chapter':leatest[0][1],
                      'latest_chapter_url':self.book_url + leatest[0][0],
                      'novel_chapter_info':{}}
        for i in leatest:
            novel_dict['novel_chapter_info'][i[1]] = self.book_url + i[0]
        print(novel_dict)
        return novel_dict

    def chapter_info(self):
        titl_re = '<h1>(.*?)</h1>'
        content_re = '<div id="content">(.*?)</div>'
        name_url_re = '<a href="/">.*?</a>.*?<a href="(.*?)">(.*?)</a>'
        pre_next_re = '</a> <a href="(.*?)">上一章</a> &larr; <a href=".*?">章节目录</a> &rarr; <a href="(.*?)">下一章</a>'
        titl = re.findall(titl_re,self.html)[0]
        content = re.findall(content_re,self.html)[0].replace('&nbsp;',' ').replace('<br /><br />','\n')
        name = re.findall(name_url_re,self.html)[0][1]
        url = re.findall(name_url_re,self.html)[0][0]
        pre = re.findall(pre_next_re,self.html)[0][0]
        next = re.findall(pre_next_re,self.html)[1][1]
        chapter_info = {'chapter_name':titl,
                        'chapter_content':content,
                        'novel_name':name,
                        'novel_author':None,
                        'novel_words':None,
                        'novel_catalog':url,
                        'pre_chapter':url + pre,
                        'next_chapter':url + next}

        return chapter_info

if __name__ == '__main__':
    a = Booktxt(chapter_url='https://www.booktxt.net/2_2219/4584821.html')
    # print(a.novel_info())